package com.ssm.controller;

import com.ssm.pojo.Log;
import com.ssm.pojo.Manager;
import com.ssm.pojo.SManager;
import com.ssm.service.ManagerService;
import com.ssm.service.SManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class LogController {

    @Autowired
    SManagerService sManagerService;

    @Autowired
    ManagerService managerService;

    @RequestMapping("/login")
    public String toLogin() {
        return "login";
    }

    @RequestMapping("/onLogin")
    public String onLogin(Log judge, HttpServletRequest request, HttpSession session) {
        if(judge==null){
            request.setAttribute("msg","账号或密码错误");
            return "login";
        }

        switch(judge.getManager()){
            case 1:
                List<Manager> list = managerService.list();
                for (int i = 0; i < list.size(); i++) {
                    Manager manager = list.get(i);
                    if (manager.getAmount().equals(judge.getAmount()) && manager.getPassword().equals(judge.getPassword())) {
                        session.setAttribute("manager", manager);
                        request.setAttribute("msg","");
                        return "manager/managerIndex";
                    }
                };break;
            case 2:
                List<SManager> list2 = sManagerService.list();
                for (int i = 0; i < list2.size(); i++) {
                    SManager sManager = list2.get(i);
                    if (sManager.getAmount().equals(judge.getAmount()) && sManager.getSpassword().equals(judge.getPassword())) {
                        session.setAttribute("manager", sManager);
                        request.setAttribute("msg","");
                        return "smanager/smanagerIndex";
                    }
                }
        }
        request.setAttribute("msg","账号或密码错误");
        return "login";
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "login";
    }

    @RequestMapping("/re")
    public String re(){
        return "index";
    }
}
